package com.dreamer.repository.pyramid;

import com.dreamer.domain.pyramid.CashVo;
import com.dreamer.domain.pyramid.CashWithdrawal;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import ps.mx.otter.utils.SearchParameter;
import ps.mx.otter.utils.dao.hibernate.HibernateBaseDAO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Repository("cashWithdrawalDAO")
public class CashWithdrawalDAO extends HibernateBaseDAO<CashWithdrawal> {

    private Session getCurrentSession() {
        return getSessionFactory().getCurrentSession();
    }

    public List<CashWithdrawal> searchAllByPage(SearchParameter<CashWithdrawal> p) {
        return super.searchEntityByPage(p, (t) -> {
            Example example = Example.create(t.getEntity()).enableLike(MatchMode.ANYWHERE);
            Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(getClazz());
            criteria.add(example);
            return criteria;
        }, (o)-> true);
    }

    public void save(CashWithdrawal transientInstance) {

        try {
            getCurrentSession().save(transientInstance);

        } catch (RuntimeException re) {

            throw re;
        }
    }

    public void delete(CashWithdrawal persistentInstance) {
        try {
            getCurrentSession().delete(persistentInstance);

        } catch (RuntimeException re) {

            throw re;
        }
    }

    public CashWithdrawal findById(Integer id) {

        try {
            CashWithdrawal instance = (CashWithdrawal) getCurrentSession().get(
                    "com.dreamer.domain.pyramid.CashWithdrawal", id);
            return instance;
        } catch (RuntimeException re) {

            throw re;
        }
    }

    public CashWithdrawal merge(CashWithdrawal detachedInstance) {
        try {
            CashWithdrawal result = (CashWithdrawal) getCurrentSession().merge(detachedInstance);
            return result;
        } catch (RuntimeException re) {

            throw re;
        }
    }

    public List<CashWithdrawal> findAll() {
        try {
            String queryString = "from CashWithdrawal";
            Query queryObject = getCurrentSession().createQuery(queryString);
            return queryObject.list();
        } catch (RuntimeException re) {
            throw re;
        }
    }

    public List<CashWithdrawal> getCashWithdrawal(Integer userId) {
        try {
            String queryString = "from CashWithdrawal c where c.user.id = :userId order by c.createTime desc";
            Query queryObject = getCurrentSession().createQuery(queryString);
            queryObject.setParameter("userId",userId);
            return queryObject.list();
        } catch (RuntimeException re) {
            throw re;
        }
    }

    //todo 后台处理提现
    public List<CashVo> findCashVoAll(SearchParameter<CashVo> p) {
        List<CashVo> cashVoList = new ArrayList<>();
        StringBuffer stringBuffer = new StringBuffer();
        String hql = " SELECT " +
                " cw.id as id," +
                "cw.userId as userId," +
                "u.realName as realName," +
                "u.mobile as mobile," +
                "cw.cardNumber as cardNumber," +
                "cw.bankName as bankName," +
                "cw.amountMoney as amountMoney," +
                "cw.poundage as poundage," +
                "cw.voucher as voucher," +
                "cw.money as money," +
                "cw.createTime as createTime," +
                "cw.status as status," +
                "cw.reason as reason," +
                "cw.name as name," +
                "cw.handleTime as handleTime," +
                "cw.info as info " +
                " from CashWithdrawal cw " +
                " left join User u on u.id = cw.userId" +
                " where 1=1  ";
        stringBuffer.append(hql);
        if(!p.getEntity().getRealName().trim().equals("") && p.getEntity().getRealName() != null){
            String hql1 = " and u.realName like '%"+p.getEntity().getRealName()+"%' ";
            stringBuffer.append(hql1);
        }
        if(!p.getEntity().getMobile().trim().equals("") && p.getEntity().getMobile() != null) {
            String hql2 = " and u.mobile like '%" + p.getEntity().getMobile() + "%' ";
            stringBuffer.append(hql2);
        }
        if(!p.getEntity().getBankName().trim().equals("") && p.getEntity().getBankName() != null){
            String hql3 = " and cw.bankName like '%" + p.getEntity().getBankName() + "%' ";
            stringBuffer.append(hql3);
        }
        if(!p.getEntity().getCardNumber().trim().equals("") && p.getEntity().getCardNumber() != null){
            String hql4 = " and cw.cardNumber like '%" + p.getEntity().getCardNumber() + "%' ";
            stringBuffer.append(hql4);
        }
        Query query = getCurrentSession().createQuery(stringBuffer.toString());
        List list = query.list();
        for (int i = 0; i < list.size(); i++) {
            Object[] object = (Object[]) list.get(i);
            CashVo cashVo = new CashVo();
            cashVo.setId((Integer) object[0]);
            cashVo.setUserId((Integer) object[1]);
            cashVo.setRealName((String) object[2]);
            cashVo.setMobile((String) object[3]);
            cashVo.setCardNumber((String) object[4]);
            cashVo.setBankName((String) object[5]);
            cashVo.setAmountMoney((Double) object[6]);
            cashVo.setPoundage((Double) object[7]);
            cashVo.setVoucher((Double) object[8]);
            cashVo.setMoney((Double) object[9]);
            cashVo.setCreateTime((Date) object[10]);
            cashVo.setStatus((Integer) object[11]);
            cashVo.setReason((String) object[12]);
            cashVo.setName((String) object[13]);
            cashVo.setHandleTime((Date) object[14]);
            cashVo.setInfo((String) object[15]);
            cashVoList.add(cashVo);
        }
        return cashVoList;
    }

    public CashVo findByIdVo(Integer id) {
        try {
            String sql = " SELECT " +
                    " cw.id as id," +
                    "cw.userId as userId," +
                    "u.realName as realName," +
                    "u.mobile as mobile," +
                    "cw.cardNumber as cardNumber," +
                    "cw.bankName as bankName," +
                    "cw.amountMoney as amountMoney," +
                    "cw.poundage as poundage," +
                    "cw.voucher as voucher," +
                    "cw.money as money," +
                    "cw.createTime as createTime," +
                    "cw.status as status," +
                    "cw.reason as reason," +
                    "cw.name as name," +
                    "cw.handleTime as handleTime," +
                    "cw.info as info " +
                    " from CashWithdrawal cw " +
                    " left join User u on u.id = cw.userId " +
                    " where 1=1  and cw.id = "+id;
            Query query = getCurrentSession().createQuery(sql);
            List list = query.list();
            CashVo cashVo = new CashVo();
            for (int i = 0; i < list.size(); i++) {
                Object[] object = (Object[]) list.get(i);
                cashVo.setId((Integer) object[0]);
                cashVo.setUserId((Integer) object[1]);
                cashVo.setRealName((String) object[2]);
                cashVo.setMobile((String) object[3]);
                cashVo.setCardNumber((String) object[4]);
                cashVo.setBankName((String) object[5]);
                cashVo.setAmountMoney((Double) object[6]);
                cashVo.setPoundage((Double) object[7]);
                cashVo.setVoucher((Double) object[8]);
                cashVo.setMoney((Double) object[9]);
                cashVo.setCreateTime((Date) object[10]);
                cashVo.setStatus((Integer) object[11]);
                cashVo.setReason((String) object[12]);
                cashVo.setName((String) object[13]);
                cashVo.setHandleTime((Date) object[14]);
                cashVo.setInfo((String) object[15]);
            }
            CashVo instance = cashVo;
            return instance;
        } catch (RuntimeException re) {
            throw re;
        }
    }

}
